/**
 * Created by Administrator on 2017/3/22 0022.
 */
var Line={
    create:function () {
        var line={};
        var geometry,material,mesh,points;
        var size,index;
        var fftData;
        //var cColor;

        line.init=function (_y,_index,_size,_fftData) {
            console.log('line!!!!!')
            size = _size;
            index = _index;
            fftData=_fftData;
            points = line.generatedPoints(_fftData);

            var _color=Math.floor(255/_index)<<16;
            // console.log(255/_index);
            var haha=index*10;
            // console.log();
            // var _color=(255<<16)+(haha<<8)+0;

            geometry= new THREE.Geometry();
            geometry.vertices = points;

            var _color=0xffffff;
            line.color=_color;

            material = new THREE.LineBasicMaterial({color: _color,linewidth:3});
            //material.color.setHex( Math.random() * 0xffffff );
            line.mesh = new THREE.Line(geometry, material);
            var twY=-20 + (_y || 0);
            line.mesh.position.y   = twY+250
            line.mesh.position.z   = (12 - index) * -2 || 0;

            console.log('line!!!!!')
            TweenMax.to(line.mesh.position,1,{delay:index*0.1,y:twY,ease:Back.easeOut});
            TweenMax.to(material,1,{delay:index*0.1,opacity:1,ease:Back.easeOut});
            // this.counter           = 0;


        }
        line.out=function()
        {
            TweenMax.to(line.mesh.position,1,{delay:index*0.1,y:line.mesh.position.y+250,ease:Back.easeIn});
            TweenMax.to(material,1,{delay:index*0.1,opacity:0,ease:Back.easeIn});
        }
        line.changeColor=function(hex)
        {
            TweenMax.to(line,0.1,{color:hex,onUpdate:function()
            {
                material.color.setHex(line.color)
            },ease:Linear.easeNone});
        }

        line.generatedPoints=function (array) {
            if(!array) return;

            var i;

            var straightLines = generatePerlinNoise(1, 16);
            for (i = straightLines.length - 1; i >= 0; i--) {
                straightLines[i] *= 10;
            };

            // console.log(array)

            var noise = [];
            var order = index < 11 ? 10 - index : index;

            var range = 512 / 8;
            var start = range * (order % 3);

            for (i = range + start; i >= start; i--) {
                var a = array[i] / 5;
                a = a > 20 ? a * 1.5 : a / 50;
                a = Math.max(5, a);
                noise.push(a);
            };

            noise = straightLines.concat(noise);
            var invertedPerlin = noise.slice(0);
            invertedPerlin = invertedPerlin.reverse()
            noise = noise.concat(invertedPerlin);

            var arr_spline = [];
            i = 0;

            var ratio = size.x / noise.length;

            while(i<noise.length){
                arr_spline.push(new THREE.Vector3( -size.x/2 + (ratio * i), noise[i], 0 ) )
                i++;
            };

            var curve = new THREE.CatmullRomCurve3  (arr_spline);
            return curve.getPoints(511);

            // return arr_spline;
        }
        line.update=function(_fftData) {
            // return
            if(!geometry.vertices)
            {

                return;
            }

            points = line.generatedPoints( _fftData );

            // console.log(points[0],points[1],points[2])

            for (var i = geometry.vertices.length-1; i >= 0; i--) {
                geometry.vertices[i].y += (points[i].y -geometry.vertices[i].y) * 0.2

            };

            line.mesh.geometry.verticesNeedUpdate = true
        }

        return line;
    }
}